// pages/productDetail/productDetail.js
const app = getApp()
var postLogin = require("../../utils/wh.js");
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    border: false,
    productID: "",
    url:"",
    product: [ ],//产品详情
    indicatorDots: true,//轮播圆点
    autoplay: true,//是否自动切换
    interval: 5000,//自动切换时间
    duration: 1000,//滑动动画时长
    indicatorActiveColor: '#ffffff',
    imgUrls: [ ],
    setting:{}
  },
  // 返回到首页
  goHome: function () {
    wx.switchTab({
      url: '../index/index'
    })
  },

  border: function () {
    var that = this;
    this.setData({
      border: !that.data.border
    })
  },
  //获取小程序设置
  get_app_setting: function () {
    let that = this;
    postLogin.Post('/api/setting/get', {}, function (res) {
      that.setData({ setting: res });
    })
  },
  // 打电话
  call:function(){

    let that = this;
    let setting = that.data.setting;
    let tell = setting.service_tel;

    if (tell == '' || tell == undefined) {

      wx.showModal({
        title: '提示', content: '联系电话未设置，不能联系', showCancel: false
      })
      return;
    }
    wx.makePhoneCall({
      phoneNumber: tell,
    })
  },
  goContactUs:function(){
    wx.navigateTo({
      url: '../contactUs/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      productID: options.id
    })
    wx.showLoading({
      title: '加载中•••',
      mask: true
    })
    this.loop();
    this.get_app_setting();

  },
  loop: function () {
    var that = this;
    var code = app.globalData.code;
    var url = app.globalData.URL;
    if (!code || !url) {
      var that = this
      setTimeout(function () { that.loop(); }, 100)
    } else {
      that.setData({
        url: url,
      })
      var postUrl = '/api/company.index/productContent',
        postData = {
          product_id: that.data.productID
        };
      postLogin.Post(postUrl, postData, that.homeData);
    }
  },
  homeData: function (res) {
    let that = this;
    wx.hideLoading();
    var productContent = res;
    var bannerURL = res.product_album
    that.setData({
      product: productContent,
      imgUrls: bannerURL
    })
    console.log(res.product_content);
    WxParse.wxParse('content', 'html', res.product_content, that);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})